package org.coco.mybatis.session;


import com.mysql.fabric.proto.xmlrpc.ResultSetParser;
import org.coco.jdbc.SqlSessionFactory;
import org.coco.mybatis.Configuration;
import org.coco.mybatis.binding.MethodSignature;
import org.coco.mybatis.mapping.BoundSql;
import org.coco.mybatis.mapping.ResultMap;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;

public class DefaultSqlSession implements SqlSession {

    private static Configuration configuration;
    private static SqlSessionFactory sqlSessionFactory;

    static {
        sqlSessionFactory = new SqlSessionFactory();
        configuration = Configuration.getInstance();
    }

    @Override
    public <T> T selectOne(String statement) {
        return null;
    }

    @Override
    public <E> List<E> selectList(BoundSql boundSql) throws Exception{
        List<E> list = null;
        Connection connection = null;
        ResultSet resultSet = null;
        PreparedStatement ps = null;
        try{
            connection = configuration.getDataSource().getConnection();
            ps = connection.prepareStatement(boundSql.getSql());
            //开始填补参数
            List<MethodSignature> params = boundSql.getParams();
            int var0 = params.size();
            MethodSignature methodSignature = null;
            for(int i = 0; i < var0; i ++) {
                methodSignature = params.get(i);
                Class<?> clazz = methodSignature.getClazz();
                if(clazz == String.class) {
                    ps.setString(i+1,String.valueOf(methodSignature.getValue()));
                }else if(clazz == Integer.class || clazz == int.class) {
                    ps.setInt(i+1,Integer.parseInt(String.valueOf(methodSignature.getValue())));
                }
            }
            resultSet = ps.executeQuery();
            //开始遍历
            ResultParse resultParse = new ResultParse();
            list = resultParse.parseForMany(resultSet, boundSql);
        }catch (Exception  e) {
            
        }finally {
            SqlSessionFactory.releaseSqlConnection(resultSet,ps,connection);
        }
        return list;
    }

    @Override
    public Integer delete(BoundSql boundSql) {
        Connection connection = null;
        ResultSet resultSet = null;
        PreparedStatement ps = null;
        try{
            connection = configuration.getDataSource().getConnection();
            ps = connection.prepareStatement(boundSql.getSql());
            //开始填补参数
            List<MethodSignature> params = boundSql.getParams();
            int var0 = params.size();
            MethodSignature methodSignature = null;
            for(int i = 0; i < var0; i ++) {
                methodSignature = params.get(i);
                Class<?> clazz = methodSignature.getClazz();
                if(clazz == String.class) {
                    ps.setString(i+1,String.valueOf(methodSignature.getValue()));
                }else if(clazz == Integer.class || clazz == int.class) {
                    ps.setInt(i+1,Integer.parseInt(String.valueOf(methodSignature.getValue())));
                }
            }
            int i = ps.executeUpdate();
            return i;
        }catch (Exception  e) {

        }finally {
            SqlSessionFactory.releaseSqlConnection(resultSet,ps,connection);
        }
        return 0;
    }

    @Override
    public Integer insert(BoundSql boundSql) {
        Connection connection = null;
        ResultSet resultSet = null;
        PreparedStatement ps = null;
        try{
            connection = configuration.getDataSource().getConnection();
            ps = connection.prepareStatement(boundSql.getSql());
            //开始填补参数
            List<MethodSignature> params = boundSql.getParams();
            int var0 = params.size();
            MethodSignature methodSignature = null;
            for(int i = 0; i < var0; i ++) {
                methodSignature = params.get(i);
                Class<?> clazz = methodSignature.getClazz();
                if(clazz == String.class) {
                    ps.setString(i+1,String.valueOf(methodSignature.getValue()));
                }else if(clazz == Integer.class || clazz == int.class) {
                    ps.setInt(i+1,Integer.parseInt(String.valueOf(methodSignature.getValue())));
                }
            }
            int i = ps.executeUpdate();
            return i;
        }catch (Exception  e) {

        }finally {
            SqlSessionFactory.releaseSqlConnection(resultSet,ps,connection);
        }
        return 0;
    }
}
